package com.cqjtu.drive.service.impl;

import com.cqjtu.drive.mapper.EnrollMapper;
import com.cqjtu.drive.mapper.GradeMapper;
import com.cqjtu.drive.mapper.StudentMapper;
import com.cqjtu.drive.pojo.Course;
import com.cqjtu.drive.pojo.Enroll;
import com.cqjtu.drive.pojo.Grade;
import com.cqjtu.drive.pojo.Student;
import com.cqjtu.drive.response.Response;
import com.cqjtu.drive.service.EnrollService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class EnrollServiceImpl implements EnrollService {

    @Autowired
    private EnrollMapper enrollMapper;

    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private GradeMapper gradeMapper;
    @Override
    public Response<Enroll> getEnrollById(String enId) {
        Enroll enroll = enrollMapper.getEnrollById(enId);
        if (enroll != null) {
            return new Response<>(true, "Success", enroll);
        } else {
            return new Response<>(false, "Enroll not found", null);
        }
    }

    @Override
    public Response<String> insertEnroll(Enroll enroll) {
        Student student = studentMapper.getStudentById(enroll.getsId());
        if(student.getsDay() < 10)
            return new Response<>(false, "学时不够" );
        try {
            List<Enroll> list = enrollMapper.getAllEnrolls();
            if(list != null && !list.isEmpty()){
                String id = Integer.toString(Integer.parseInt(list.get(list.size() - 1).getEnId()) + 1);
                enroll.setEnId(id);
            }else {
                enroll.setEnId("1");
            }
            enrollMapper.insertEnroll(enroll);
            return new Response<>(true, "Insert success", null);
        } catch (Exception e) {
            return new Response<>(false, "Insert failed: " + e.getMessage(), null);
        }
    }

    @Override
    public Response<String> updateEnroll(Enroll enroll) {
        try {
            enrollMapper.updateEnroll(enroll);
            return new Response<>(true, "Update success", null);
        } catch (Exception e) {
            return new Response<>(false, "Update failed: " + e.getMessage(), null);
        }
    }

    @Override
    public Response<String> deleteEnroll(String enId) {
        try {
            enrollMapper.deleteEnroll(enId);
            return new Response<>(true, "Delete success", null);
        } catch (Exception e) {
            return new Response<>(false, "Delete failed: " + e.getMessage(), null);
        }
    }

    @Override
    public Response<List<Enroll>> getAllEnrolls() {
        List<Enroll> enrolls = enrollMapper.getAllEnrolls();
        return new Response<>(true, "Success", enrolls);
    }

    @Override
    public Response<List<Enroll>> getAllEnrollsWithAccept0() {
        List<Enroll> enrolls=enrollMapper.getAllEnrollsWithAccept0();
        return new Response<>(true, "Success", enrolls);
    }

    @Override
    public Response<List<Enroll>> getAllEnrollsWaitRecord() {
        List<Enroll> enrolls=enrollMapper.getAllEnrollsWaitRecord();
        return new Response<>(true, "Success", enrolls);
    }

    @Override
    public Response<String> updateEnrollAccept(String enId, String newAccept) {
        try {
            enrollMapper.updateEnrollAccept(enId,newAccept);
            return new Response<>(true, "修改成功", null);
        } catch (Exception e) {
            return new Response<>(false, "update failed: " + e.getMessage(), null);
        }
    }

    @Override
    public Response<List<Enroll>> getAllEnrollsBySId(String sId) {
        List<Enroll> enrolls=enrollMapper.getAllEnrollsBySId(sId);
        return new Response<>(true, "Success", enrolls);
    }

    @Override
    public Response<Enroll> getNewestEnrollsBySId(String sId) {
        Enroll e=null;
        List<Enroll> enrolls=enrollMapper.getAllEnrollsBySId(sId);
        for (Enroll s:enrolls){
            if (s.getsId().equals(sId)) e=s;
        }
        if(e==null){
            return new Response<>(false,"该学生没有预约记录",null);
        }else {
            return new Response<>(true, "Success", e);
        }
    }

    @Override
    public Response<String> recordGrade(Map<String, String> map) {
        try{
            String enId=map.get("enId");
            String sId=map.get("sId");
                Integer grade=Integer.parseInt(map.get("grade"));
            String type=map.get("enType");
            Enroll enroll=enrollMapper.getEnrollById(enId);
            enroll.setEnGrade(grade);
            enroll.setEnRecord(1);
            enrollMapper.updateEnroll(enroll);
            Grade nowGrade= gradeMapper.getGradeById(sId);
            switch (type) {
                case "1":
                    nowGrade.setgGrade1(grade);
                    break;
                case "2":
                    nowGrade.setgGrade2(grade);
                    break;
                case "3":
                    nowGrade.setgGrade3(grade);
                    break;
                case "4":
                    nowGrade.setgGrade4(grade);
                    break;
            }
            gradeMapper.updateGrade(nowGrade);
            Student student = studentMapper.getStudentById(sId);
            if (grade >= 90) {
                student.setsAccept(student.getsAccept() + 1);
                student.setsDay(0);
                student.setsTestnum(0);
                studentMapper.updateStudent(student);
            }else {
                int num=student.getsTestnum()+1;
                if(num>=5){
                    student.setsAccept(0);
                    student.setsDay(0);
                    student.setsTestnum(0);
                    studentMapper.updateStudent(student);
                }else{
                    student.setsTestnum(student.getsTestnum()+1);
                    studentMapper.updateStudent(student);
                }
            }
            return new Response<>(true, "Success", null);
        }catch (Exception e){
            return new Response<>(false, "false "+e.getMessage(), null);
        }
    }
}
